package org.spark.core.action.java;

import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.spark.sparkUtil.SparkJavaContextUtil;
import scala.Tuple2;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 功能概述:
 * Datetime:    2020/5/28   19:23
 * Author:   某人的目光
 */
public class CountByKey {
    public static void main(String[] args) {
        JavaSparkContext sc = SparkJavaContextUtil.getSparkStart("CountByKey");
        // 模拟集合
        List<Tuple2<String, String>> scoreList = Arrays.asList(
                new Tuple2<String, String>("class1", "leo"),
                new Tuple2<String, String>("class2", "jack"),
                new Tuple2<String, String>("class1", "marry"),
                new Tuple2<String, String>("class2", "tom"),
                new Tuple2<String, String>("class2", "david"));

        // 并行化集合，创建JavaPairRDD
        JavaPairRDD<String, String> students = sc.parallelizePairs(scoreList);

        // 对rdd应用countByKey操作，统计每个班级的学生人数，也就是统计每个key对应的元素个数
        // 这就是countByKey的作用
        // countByKey返回的类型，直接就是Map<String, Object>
        Map<String, Long> studentCounts = students.countByKey();

        for (Map.Entry<String, Long> studentCount : studentCounts.entrySet()) {
            System.out.println(studentCount.getKey() + ": " + studentCount.getValue());
        }

        // 关闭JavaSparkContext
        sc.close();
    }
}
